package com.ljsw.dialog;

import java.util.ArrayList;

import com.ljsw.activity.R;
import com.ljsw.adapter.ZhouzxAdapter;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

/**
 * 领取任务谈出的Dialog  
 * 作者: 石锚
 * 版本: 1.0
 * 日期: 2015-10-15
 */
public class RwlqDialog {
	
	public RwlqDialog(Context context) {
		super();
		this.context = context;
		
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		display = windowManager.getDefaultDisplay();//获取屏幕分辨率
	}
	private ArrayList<String> list = new ArrayList<String>();
	private Context context;
	private Dialog dialog;
	private Display display;
	private LinearLayout lay_lqrw,lay_lqrw_smzzx;
	private TextView qd_rwlq,qx_rwlq,//确定|取消
					 qd_rwlq_smzzx,qx_rwlq_smzzx;
	private ListView rwlq_smzzx_list;
	
	private View view;
	/**
	 * 加载任务领取的Dialog 布局
	 * @return
	 */
	public RwlqDialog builder(){
		view = LayoutInflater.from(context).inflate(R.layout.dialog_lqrw, null);
		lay_lqrw = (LinearLayout) view.findViewById(R.id.lay_lqrw);
		qd_rwlq =  (TextView) view.findViewById(R.id.qd_rwlq);
		qx_rwlq =  (TextView) view.findViewById(R.id.qx_rwlq);
		// 添加Dialog样式
		dialog = new Dialog(context, R.style.RwlqDialog);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);// 设置点击Dialog外部任意区域不关闭Dialog	
		// 调整dialog背景大小
		lay_lqrw.setLayoutParams(new FrameLayout.LayoutParams((int) (display
				.getWidth() * 0.85), LayoutParams.WRAP_CONTENT));				
		return this;
	}
	
	/**
	 * 确定事件
	 * @param text
	 * @param listener
	 * @return
	 */
	public RwlqDialog setPositiveButton(String text,
			final OnClickListener listener) {
		
		qd_rwlq.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				listener.onClick(v);
				dialog.dismiss();
			}
		});
		return this;
	}
	/**
	 * 取消事件
	 * @param text
	 * @param listener
	 * @return
	 */
	public RwlqDialog setNegativeButton(String text,
			final OnClickListener listener) {
		qx_rwlq.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				listener.onClick(v);
				dialog.dismiss();
			}
		});
		return this;
	}
	
	
	/**
	 * 加载任务领取的Dialog 布局
	 * @return
	 */
	public RwlqDialog builder2(){
		view = LayoutInflater.from(context).inflate(R.layout.dialog_lqrw_smzzx, null);
		lay_lqrw_smzzx = (LinearLayout) view.findViewById(R.id.lay_lqrw_smzzx);
		rwlq_smzzx_list = (ListView) view.findViewById(R.id.rwlq_smzzx_list);
		qd_rwlq_smzzx =  (TextView) view.findViewById(R.id.qd_rwlq_smzzx);
		qx_rwlq_smzzx =  (TextView) view.findViewById(R.id.qx_rwlq_smzzx);
		// 添加Dialog样式
		dialog = new Dialog(context, R.style.RwlqDialog);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);// 设置点击Dialog外部任意区域不关闭Dialog
		// 调整dialog背景大小
		lay_lqrw_smzzx.setLayoutParams(new FrameLayout.LayoutParams((int) (display
				.getWidth() * 0.85), LayoutParams.WRAP_CONTENT));				
		return this;
	}
	
	/**
	 * 确定事件
	 * @param text
	 * @param listener
	 * @return
	 */
	public RwlqDialog setPositiveButton2(String text,
			final OnClickListener listener) {
		qd_rwlq_smzzx.setOnClickListener(new OnClickListener() {	
			public void onClick(View v) {
				listener.onClick(v);
				dialog.dismiss();
			}
		});
		return this;
	}
	/**
	 * 取消事件
	 * @param text
	 * @param listener
	 * @return
	 */
	public RwlqDialog setNegativeButton2(String text,
			final OnClickListener listener) {
		qx_rwlq_smzzx.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				listener.onClick(v);
				dialog.dismiss();
			}
		});
		return this;
	}
	private ZhouzxAdapter adapter;
	/**
	 * 往集合添加扫描到的数据
	 */
	public void addBox(String box){
		list.add(box);
	}
	/**
	 * 初始化适配器
	 */
	public void creatAdapter(){
		adapter = new ZhouzxAdapter(context,list);
	}
	/**
	 * 绑定适配器
	 */
	public void setAdapter(){
		rwlq_smzzx_list.setAdapter(adapter);
	}
	/**
	 * 刷新适配器
	 */
	public void notifyDataSetChanged(){
		adapter.notifyDataSetChanged();
	}
	/**
	 * Dialog 显示
	 */
	public void show() {
		dialog.show();
	}
}
